using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Services.Menu
{
    public class LogoScreen : DrawableGameComponent
    {
        SpriteBatch sprite_batch;
        
        Texture2D logo;
        Rectangle texture_rectangle;
        Matrix screen_transform;
        string logo_path;
        float alpha;
        AStateManager StateManager;
        Game game;
        #region Constructors
        GraphicsDeviceManager graphics;
        public LogoScreen(Game game, string logo_path, GraphicsDeviceManager graphics)
            : base(game)
        {
            alpha = 0.0f;
            this.logo_path = logo_path;
            this.game = game;
            this.graphics = graphics;
        }

        #endregion

        public override void Initialize()
        {
            sprite_batch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            StateManager = (AStateManager)game.Services.GetService(typeof(AStateManager));
            screen_transform = Game.ScreenTransformMatrix(graphics);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            logo = Game.Content.Load<Texture2D>(logo_path);
            texture_rectangle = new Rectangle(Game.GraphicsDevice.Viewport.Width / 2 - logo.Width / 2,
                Game.GraphicsDevice.Viewport.Height / 2 - logo.Height / 2,
                logo.Width, logo.Height);

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

            alpha += dt;

            if (alpha > 2.0f) StateManager.SelectMainMenu();

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            sprite_batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, screen_transform);
            sprite_batch.Draw(logo, texture_rectangle, Color.White * alpha);

            sprite_batch.End();

            base.Draw(gameTime);
        }
    }
}
